/* 不带优先级声明的中缀表达式计算器 */

%{
#define YYSTYPE double
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int yylex (void);
void yyerror (char const *);
%}

%token T_NUM
%token T_ADD T_SUB T_MUL T_DIV T_POW T_LEFT_PT T_RIGHT_PT
%token EOL



%% /* Grammar rules and actions follow. */

input:
	input line
    | /* empty */ {printf("empty\n");}
	;

line: EOL   {printf("newline\n");}
	| exp0 EOL { printf ("\t%.10g\n", $1); }
	;

exp0: exp1 { $$ = $1; } 
   	| exp0 T_ADD exp1 { $$ = $1 + $3; }
   	| exp0 T_SUB exp1 { $$ = $1 - $3; }
   
   ;
exp1: exp2 { $$ = $1; } 
   	| exp1 T_MUL exp2 { $$ = $1 * $3; }
   	| exp1 T_DIV exp2 { $$ = $1 / $3; }
   ;

exp2: exp3 { $$ = $1; }
	| exp3 T_POW exp2 { $$ = pow($1, $3); }		//右结合

exp3: exp4 { $$ = $1; }
	| T_SUB exp4 { $$ = -$2; }
	;

exp4: T_NUM { $$ = $1; }
	| T_LEFT_PT exp0 T_RIGHT_PT { $$ = $2; }
    ;

%%

int main (void)
{
	return yyparse();
}


void yyerror (char const *s)
{
	fprintf (stderr, "%s\n", s);
}